package ru.spbu.math.android.cbrf.dataprovider.yandex;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import ru.spbu.math.android.cbrf.R;
import ru.spbu.math.android.cbrf.model.Category;
import ru.spbu.math.android.cbrf.model.RateType;
import ru.spbu.math.android.cbrf.model.StockItem;
import android.content.res.Resources;

/**
 * Class for parsing data about all available RateTypes for Yandex and put it in the List of RateType.
 * This class extends DefaultHandler
 * @author  Svetlana Zemlyanskaya
 */

//<RateType>
//	<category>Metal</category>
//	<id>10</id>
//	<code>AU</code>
//	<name>Gold</name>
//	<nominalValue>1</nominalValue>
//	<nominalName>Troy ounce</nominalName>
//	<target>USD</target>
//	<targetName>US Dollar</targetName>
//</RateType>

public class RateTypeSaxHandler extends DefaultHandler {

	private static String oilCategory;
	private static String metalCategory;
	private static String crossCategory;
	private static String marketCategory;
	private static String stockCategory;
	
	private final String RATE_TYPE = "RateType";
    private final String CATEGORY = "category";
    private final String ID = "id";
	private final String CODE = "code";
    private final String NAME = "name";
    private final String NOMINALVALUE = "nominalValue";
    private final String NOMINALNAME = "nominalName";
    private final String TARGET = "target";
    private final String TARGET_NAME = "targetName";
    
    private Resources resources;
    
    private List<RateType> oilList;
    private List<RateType> metalList;
    private List<RateType> crossList;
    private List<RateType> marketList;
    private List<RateType> stockList;
    
    private Category category;
    private String id = "";
    private RateType rateType;
    private StockItem source;
    private String targetCode = "";
    private String targetName = "";
    
    private StringBuilder builder;

    
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        builder.append(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
    	if (localName.equalsIgnoreCase(CATEGORY)) {
    		this.category = new Category(builder.toString().trim());
    	}
    	else if(localName.equalsIgnoreCase(ID)){
    		id = builder.toString().trim();
    	}
    	else if(localName.equalsIgnoreCase(CODE)){
    		source.setCode(builder.toString().trim());
    	}
    	else if(localName.equalsIgnoreCase(NAME)){
    		source.setName(builder.toString().trim());
    	}
    	else if(localName.equalsIgnoreCase(NOMINALVALUE)){
    		String str = builder.toString().trim();
    		source.setNominalValue(Integer.parseInt(str));
    	}
    	else if(localName.equalsIgnoreCase(NOMINALNAME)){
    		source.setNominalName(builder.toString().trim());
    	}
    	else if(localName.equalsIgnoreCase(TARGET)){
    		targetCode = builder.toString().trim();
    	} 
    	else if(localName.equalsIgnoreCase(TARGET_NAME)){
    		targetName = builder.toString().trim();
    	}
    	else if(localName.equalsIgnoreCase(RATE_TYPE)){
    		StockItem target;
    		if(targetCode.equals("RUB")){
    			target = new StockItem("RUB", targetName, 1, "");
    		}
    		else if(targetCode.equals("USD")){
				target = new StockItem("USD", targetName, 1, "");
			}
			else if(targetCode.equals("CHF")){
				target = new StockItem("CHF", targetName, 1, "");
			}
			else if(targetCode.equals("EUR")){
				target = new StockItem("EUR", targetName, 1, "");
			}
			else if(targetCode.equals("JPY")){
				target = new StockItem("JPY", targetName, 1, "");
			}
			else{
				target = new StockItem("", "", 1, "");
			}
			rateType = new RateType(source, target, id);
    		if(category.getName().equals(oilCategory)){
    			oilList.add(rateType);
    		}
    		else if(category.getName().equals(metalCategory)){
    			metalList.add(rateType);
    		}
    		else if(category.getName().equals(crossCategory)){
    			crossList.add(rateType);
    		}
    		else if(category.getName().equals(marketCategory)){
    			marketList.add(rateType);
    		}
    		else if(category.getName().equals(stockCategory)){
    			stockList.add(rateType);
    		}
    	}	
    	builder.setLength(0);
    }

    @Override
    public void startDocument() throws SAXException {
		oilCategory = resources.getString(R.string.Yandex_Oil);
		metalCategory = resources.getString(R.string.Yandex_Metal);
		crossCategory = resources.getString(R.string.Yandex_Cross);
		marketCategory = resources.getString(R.string.Yandex_Market);
		stockCategory = resources.getString(R.string.Yandex_Stock);
        oilList = new ArrayList<RateType>();
        metalList = new ArrayList<RateType>();
        crossList = new ArrayList<RateType>();
        marketList = new ArrayList<RateType>();
        stockList = new ArrayList<RateType>();
        builder = new StringBuilder();
    }

    @Override
    public void startElement(String uri, String localName, String name,
                             Attributes attributes) throws SAXException {
        if (localName.equalsIgnoreCase(RATE_TYPE)) {
        	this.source = new StockItem();
        }
    }

    /**
	 * @return oil list from XML
	 */
    public List<RateType> getOil() {
        return oilList;
    }

    /**
	 * @return currency list from XML
	 */
    public List<RateType> getMetal() {
        return metalList;
    }
    
    /**
	 * @return cross-rate list from XML
	 */
    public List<RateType> getCross() {
        return crossList;
    }
    
    /**
	 * @return stock market list from XML
	 */
    public List<RateType> getMarket() {
        return marketList;
    }
    
    /**
	 * @return stock list from XML
	 */
    public List<RateType> getStock() {
        return stockList;
    }    
	
	/**
	 * Set resources
	 * @param res
	 */
	public void setResources(Resources res){
		resources = res;
	}
}